package com.zirenx.common.controller;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.SqlPara;
import com.zirenx.utils.StringUtils;

public class BaseController extends Controller
{
	protected void setBase(){
        setAttr("base", getRequest().getContextPath());     //项目部署路径
		setAttr("baseAdmin", getRequest().getContextPath()+"/admin");	//后台访问前缀
		setAttr("v", System.currentTimeMillis());
    }

    public void render(String view) {
        setBase();
        super.render(view);
    }

    private int pageNumber = 1;
    private int pageSize = 15;
    
    
    protected int getPageNumber() {
		try {
			HttpServletRequest request = getRequest();
			String offset = request.getParameter("offset") == null ? pageNumber+"" : request.getParameter("offset");
			BigDecimal a = new BigDecimal(offset);
			pageNumber = a.intValue();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pageNumber;
	}

	protected int getPageSize() {
		try {
			HttpServletRequest request = getRequest();
			String limit = request.getParameter("limit") == null ? pageSize+"" : request.getParameter("limit");
			BigDecimal b = new BigDecimal(limit);
			pageSize = b.intValue();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pageSize;
	}

	/**
	 * 追加查询参数条件
	 * @param sqlPara
	 * @param sql
	 */
	protected String appendSearch(SqlPara sqlPara){
		String sql = "";
		String searchField = get("searchField");	//查询字段
		String searchName = get("searchName");		//查询内容
		if (StringUtils.isNotEmpty(searchField)){
			sql = " and bean."+searchField+" like ?";
			sqlPara.addPara("%"+searchName+"%");
		}
		return sql;
	}
}
